//
// Created by root on 2020/3/30.
//

#ifndef HEATBEATSERVER_SERVER_H
#define HEATBEATSERVER_SERVER_H

#include <mutex>

#include <muduo/net/TcpServer.h>
#include <muduo/net/EventLoop.h>
#include <muduo/net/InetAddress.h>

#include "PackHandle.h"

class CServer {
private:
    using TcpConnectionWeakPtr = std::weak_ptr<muduo::net::TcpConnection>;

public:
    CServer(muduo::net::EventLoop *_pEventLoop, muduo::net::InetAddress& _oInetAddress);
    virtual ~CServer();
    void Start();

private:
    void onConnection(const muduo::net::TcpConnectionPtr& _pTcpConn);
    void onMessage(const muduo::net::TcpConnectionPtr& _pTcpConn, muduo::net::Buffer* _pBuffer,
                    muduo::Timestamp _oTimeStamp);
    void onCheckRegular();
    void onCheckUnLoginConn();

private:
    /**
    * 建立连接客户已经登陆
    * @param _csConnName
    */
    void _RemoveUnLoginConn(const std::string &_csConnName);

private:
    muduo::net::TcpServer m_oTcpServer;
    std::vector<std::pair<TcpConnectionWeakPtr, uint32_t >> m_vtUnLoginConn;      ///< 建立连接 但未注册的客户端
    muduo::net::EventLoop *m_pLoop = nullptr;     ///< 事件循环对象
    std::unique_ptr<CPackHandle> m_pPackHandler = nullptr;
};


#endif //HEATBEATSERVER_SERVER_H
